<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/13
 * Time: 10:59
 */

namespace app\mobilewx\controller;;


use app\Admin\controller\Goods;
use app\Model\AdModel;
use app\Model\CommodityClassifyModel;
use app\Model\GoodsModel;
use app\Model\PositionModel;
use think\Controller;
use think\Db;

//手机端首页
class Index extends Common
{
    /*
     * 获取微信信息（测试专用）
     */
    public function index2()
    {

        $redirect_uri="http%3A%2F%2Flvyide.cn%2fMobilewx%2fWxlogin%2fwx_user_data";//用户同意后去的页面（http://teashop.obay.vip/Home/wxlogin/wx_user_data）
        $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->Appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        $this->redirect($url);
    }
    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *   首页数据加载
     */
    public function index()
    {
        //茶品种--顶级分类
        $data['Comm1'] =CommodityClassifyModel::where('is_show','=',1)->where('delete_time',null)->where('parent_id','=',0)->order('sort_order','asc')->limit(4)->select();
        //首页顶部轮播图
        $data['Banner'] = AdModel::field('*')->where('pid',1)->where('enabled',1)->order('orderby')->limit(5)->select();
        //精品推荐
        $data['Recommended'] = GoodsModel::field('*')->where('is_on_sale',1)->where('is_recommend',1)->order('sort')->limit(9)->select();
        //新品上市
        $data['Newproducts'] = GoodsModel::field('*')->where('is_on_sale',1)->where('is_new',1)->order('sort')->limit(9) ->select();
        //热销商品
        $data['Sellwell'] = GoodsModel::field('*')->where('is_on_sale',1)->where('is_hot',1)->order('sort')->limit(9)->select();
        //其他
        $data['qita']=GoodsModel::field('*')->where('is_on_sale',1)->order('sort')->limit(9)->select();
        //热销商品上方的广告位1234(暂时无用)
        $data['ad_one'] = AdModel::field('*')  ->where('pid',151)->where('enabled',1)->order('orderby')->limit(1)->find();
        $data['ad_two'] = AdModel::field('*')  ->where('pid',152)->where('enabled',1)->order('orderby')->limit(1) ->find();//广告位2
        $data['ad_three'] = AdModel::field('*')->where('pid',153)->where('enabled',1)->order('orderby')->limit(1)->find();//广告位3
        $data['ad_four'] = AdModel::field('*') ->where('pid',154)->where('enabled',1)->order('orderby')->limit(1)->find();//广告位4

        //热销商品上方活动位
        //1.是否开启该活动板块
        $Position=PositionModel::find(2);
        if ($Position['is_open']==1){//开
            $huodong = AdModel::field('*')->where('pid',2)->where('enabled',1)->order('orderby')->limit(2)->select();//热销商品上方活动
            if ($huodong){//有设置活动
                $data['huo_dong']=$huodong;
                if ($data['huo_dong']->count()>=2){$data['hd_count']=2;}//有2个或以上
                else                              {$data['hd_count']=1;}
            }
        }
//        dump($data['huo_dong']->toArray());exit();

        $Search = Db::name('tp_config')->select();//热门搜索
        foreach ($Search as $k=>$v) {
            if ($v['name'] == 'hot_keywords') {
                $val = explode('|', $v['value']);
                $this->assign('Search', $val);
            }
        }
        return view('mobile@index/index3',$data);
    }

    /**
     *加载更多
     */
    public function ajaxGetMore(){
        $p = input('p');
        if ($p === 'undefined'){
            $p =1;
        }
        $favourite_goods = GoodsModel::field('*')
            ->where('is_recommend',1)
            ->where('is_on_sale',1)
            ->order('goods_id desc')
            ->page($p,10)
            ->select();
        $this->assign('favourite_goods',$favourite_goods);
//        $this->display();
        if ($favourite_goods){
            $num = array();
            foreach ($favourite_goods as $k=>$v){
                $data ='<li><a onclick="goodsinfo('.$v['goods_id'].')" title="'.$v['goods_name'].'"><div class="index_pro">';
                $data.='<div class="products_kuang">';
                $data.='<img src="'.goods_thum_images($v['goods_id'],400,400).'"></div>';
                $data.='<div class="goods_name">'.$v['goods_name'].'</div>';
                $data.='<div class="price">';
                $data.='<a onclick="goodsinfo('.$v['goods_id'].')" class="btns">';
                $data.='<img src="/assets/img/index_flow.png">';
                $data.='</a><span onclick="goodsinfo('.$v['goods_id'].')" class="price_pro">￥'.$v['shop_price'].'元</span>';
                $data.='</div></div></a></li>';
                $num[] = $data;
            }
        }

        return $num;
    }

    public function aaa(){
        goods_thum_images(95,20,200);
    }
    /*
     * 客服页
     */
    public function service()
    {
        return view('mobile@index/service');
    }
    public function service_die()
    {
        return view('mobile@index/service');
    }
}
